<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function _initialize(){
        if(CONTROLLER_NAME!='Index'){
            $this->is_session();
        }
        
    }


    public function index(){
        $this->is_session();
        $this->display();
    }


    //验证session
    private function is_session(){
        if(!isset($_SESSION['name'])){
           $this->display("Index/login");
           exit;
        }
        if(CONTROLLER_NAME!='Index'&&$_SESSION['name']!='admin'){
            $this->auth();
        }
        
    }

    //权限管理
    private function auth(){
        $manager=D("manager");
        $role=D("Role");
        $Model = D('Model');
        $res=$manager->where("username='".$_SESSION['name']."'")->find();
        if(!$res){
            echo "error";exit;
        }
        $r=$role->where("id=".$res['role_id'])->find();
        if(!$r){
            echo "error";exit;
        }

        $res1 = $Model->where("controller='".CONTROLLER_NAME."' and function='".ACTION_NAME."'")->find();
        if(!$res1){
            echo "error";exit;
        }
        if(strpos($r['model_id'], $res1['id'])===false){
            echo "error";exit;
        }
        
    }


    public function login(){
        $this->display();
    }
    
    public function dologin(){
        $user=I("request.shop_account");
        $pass=I("request.bas_password");
        $manager=D("manager");
        if($user=="admin"&&$pass=="admin111"){
            $_SESSION['name'] = 'admin';
            echo json_encode(1);exit;
        }elseif(isset($user)&&isset($pass)){
            $res=$manager->where("username='".$user."' and password='".md5($pass)."'")->find();
            if(!$res){
                echo json_encode(0);exit;
            }
            $_SESSION['name'] = $user;
            echo json_encode(1);exit;
        }else{
            echo json_encode(0);exit;
        }
    }
    //加载模块
    public function model(){
        $this->is_session();
        $Model = D('Model');
        $manager = D("manager");
        $role = D("role");
        if (isset($_SESSION['name']) && $_SESSION['name'] != 'admin') {
            $re = $manager->where("username='" . $_SESSION['name'] . "'")->find();
            if (!$re) {
                echo json_encode("error");
                exit;
            }
            $rr = $role->where("id=" . $re['role_id'])->find();
            if (!$rr) {
                echo json_encode("error");
                exit;
            }
            $in['id'] = array("in", rtrim($rr['model_id'], ','));
            $res = $Model->where($in)->select();
        } else {
            $res = $Model->where("cd_id!=0")->select();
        }

        $temp = array();
        foreach ($res as $vvv) {   //model Id
            if ($vvv['is_cd'] == 1) {  //model为显示菜单
                $temp[] = $vvv['cd_id'];
            }
        }
        $t = array_unique($temp);   //过滤重复的
        //中文情况下
        if ($_COOKIE["think_language"] == 'zh-cn') {
            $total = array();
            foreach ($t as $k => $v) {   //菜单Id
                $r = $Model->where('id=' . $v)->find();
                foreach ($res as $vv) {
                    if ($vv['cd_id'] == $v && $vv['is_cd'] == 1) {
                        $r['index'][] = $vv;

                    }
                }
                $total[] = $r;
            }
            echo json_encode($total);
        }
            //英文情况下
            if ($_COOKIE["think_language"] == 'en-us') {
                $total = array();
                foreach ($t as $k => $v) {
                    $r = $Model->where('id=' . $v)->find();
                    $r['name'] = $r['en_name'];
                    foreach ($res as $vv) {
                        $vv['name'] = $vv['en_name'] != '' ? $vv['en_name'] : '';
                        if ($vv['cd_id'] == $v && $vv['is_cd'] == 1) {
                            $r['index'][] = $vv;

                        }
                    }
                    $total[] = $r;
                }
                echo json_encode($total);
            }
    }

    
    public function clean(){
        unset($_SESSION['name']);
        $this->redirect("Index/login");
    }
    
}